const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    roomList: null,
    timeList: null,
    dayFlag: '0',
    checkDay: null,
    checkFlag: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    wx.showLoading({
      title: '载入数据中',
    })
    var that = this
    let res = await wx.cloud.database().collection("meetingRoom").doc(options.id).get({})
    let userRes = await wx.cloud.database().collection("user").where({
      _openid: app.globalData.userInfo._openid
    }).get()
    let data = res.data;
    wx.hideLoading()
    that.setData({
      roomList: data,
      timeList: data.timeList,
      dearUser: userRes.data[0]
    })
  },


  /* 显示地图 */
  gotoMap: function (e) {
    let LocationObj = e.currentTarget.dataset.location
    wx.openLocation({
      latitude: LocationObj.locateAtitude,
      longitude: LocationObj.locateLongitude,
      name: LocationObj.locateName,
      address: LocationObj.locateAddress
    })
  },

  /* 切换日期选项 */
  changeDayFlag: function (e) {
    console.log(e.currentTarget.dataset.dayflag)
    this.setData({
      dayFlag: e.currentTarget.dataset.dayflag
    })
  },


  /* 缓存日期选项 */
  checkboxChange: function (e) {

    this.setData({
      checkDay: e.detail.value,
      checkFlag: this.data.dayFlag
    })

  },
  /* 预约会议室*/
  async iNeedMeeting(e) {
    var that = this
    if (that.data.checkDay) {
      //预约需要知道会议室的id，以及预约的时间
      //预约是原子操作，不能两个交叉进行
      let roomID = that.data.roomList._id
      let checkFlag = that.data.checkFlag
      let checkDay = that.data.checkDay
      wx.navigateTo({
        url: '/pages/addAppointment/addAppointment?roomID=' + roomID + "&checkFlag=" + checkFlag + "&checkDay=" + checkDay,
      })
    } else {
      wx.showToast({
        title: '请选择时间段！',
        icon: "none"
      })
    }
  }
})